import provinces from 'china-division/dist/provinces.json'
import cities from 'china-division/dist/cities.json'
import areas from 'china-division/dist/areas.json'

// 全国省份
export const optionsProvince = provinces.map(province => ({
  label: province.name,
  value: province.code
}))
const optionsCity = {}

// 全国城市
for(let item of cities){
    const obj = {label: item.name, value: item.code}
    if(optionsCity[item.provinceCode]){
        optionsCity[item.provinceCode].push(obj)
    }else{
        optionsCity[item.provinceCode] = [obj]
    }
}
export const optionsCitys = optionsCity


const optionsArea = {}

// 全国地区
for(let item of areas){
    const obj = {label: item.name, value: item.code}
    if(optionsArea[item.provinceCode]){
        if(optionsArea[item.provinceCode][item.cityCode]){
            optionsArea[item.provinceCode][item.cityCode].push(obj)
        }else{
            optionsArea[item.provinceCode][item.cityCode] = [obj]
        }
    }else{
        optionsArea[item.provinceCode] = {}
        optionsArea[item.provinceCode][item.cityCode] = [obj]
    }
}
export const optionsAreas = optionsArea


export const codeToText = str => {
  if (!str) return ''

  const code = str.split(',')
  const province = provinces.find(province => province.code === code[0])
  const city = cities.find(city => city.code === code[1])
  const area = areas.find(area => area.code === code[2])

  if (province && city && area) {
    return `${province.name} - ${city.name} - ${area.name}`
  }
  return str
}

// 管理首页显示省市地
export const ProvincesAndCities = str => {
  if (!str) return ''

  const code = str.split(',')
  const province = provinces.find(province => province.code === code[0])

  if (province) {
    return `${province.name}`
  }
  return str
}